package com.seteam.collabcrossword.windows;

import com.seteam.collabcrossword.CrosswordGenerator;
import com.seteam.collabcrossword.exceptions.CollabCrosswordException;
import com.seteam.collabcrossword.exceptions.CoordinatesException;
import net.miginfocom.swing.MigLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;

public class Game extends JFrame {
    private final Logger logger = LoggerFactory.getLogger(Game.class);
    private Container container;
    private SearchBar searchBar;
    private CluesPane cluesPane;
    private Board board;
    private GroupLayout layout;
    private MigLayout migLayout;
    private CrosswordGenerator crosswordGenerator;

    public Game() {
        super("Collaborative Crossword");
        createCrosswordDummy();

        searchBar = new SearchBar();
        cluesPane = new CluesPane(crosswordGenerator.getCluesAcross(),
                crosswordGenerator.getCluesDown());
        board = new Board(crosswordGenerator.getSolution());
        init();
    }

    private void init() {
        container = getContentPane();
        layout = new GroupLayout(container);
        migLayout = new MigLayout("",                 // Layout Constraints
                "[grow][][grow]",   // Column constraints
                "[][shrink 1]");
        container.setLayout(migLayout);

        /**layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);

         layout.setHorizontalGroup(layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addComponent(searchBar)
         .addComponent(board)
         .addComponent(cluesPane)
         ));

         layout.setVerticalGroup(layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
         .addComponent(searchBar)
         .addComponent(board)
         .addComponent(cluesPane)
         ));*/
        container.add(searchBar, "span, wrap");
        container.add(board);
        container.add(cluesPane);

        /**
         * The code below sets minimum and maximum dimensions. It also adds
         * a component listener which listens to events where the JFrame gets
         * resized. It ensures that the size of the JFrame remains between the min
         * and max thresholds
         */
        setMinimumSize(new Dimension(800, 600));
        setMaximumSize(new Dimension(800, 600));
        setSize(new Dimension(800, 600));

        //TODO The min dimension is not working correctly. Need to fix it
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(final ComponentEvent componentEvent) {
                final Dimension dimension = Game.this.getSize();
                final Dimension maxDimension = Game.this.getMaximumSize();
                final Dimension minDimension = Game.this.getMinimumSize();

                if (dimension.width < minDimension.width) {
                    dimension.width = minDimension.width;
                }

                if (dimension.height < minDimension.height) {
                    dimension.height = minDimension.height;
                }

                if (dimension.width > maxDimension.width) {
                    dimension.width = maxDimension.width;
                }

                if (dimension.height > maxDimension.height) {
                    dimension.height = maxDimension.height;
                }

                Game.this.setSize(dimension);
            }
        });
        //pack();
        setVisible(true);
    }

    private void createCrosswordDummy() {
        try {
            crosswordGenerator = new CrosswordGenerator(13, 13);
            crosswordGenerator.generateCrossword();
        } catch (final IOException e) {
            logger.debug("Exception: {}", e.getMessage());
        } catch (final CollabCrosswordException e) {
            logger.debug("Exception: {}", e.getMessage());
        } catch (final CoordinatesException e) {
            logger.debug("Exception: {}", e.getMessage());
        }
    }
}
